/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Admin
 */
@Entity
@Table(name = "Person")
@NamedQueries({
    @NamedQuery(name = "Person.findAll", query = "SELECT s FROM Person s"),
    @NamedQuery(name = "Person.findByPersonId", query = "SELECT s FROM Person s WHERE s.id = :personId"),
    @NamedQuery(name = "Person.findByFirstName", query = "SELECT s FROM Person s WHERE s.firstName = :firstName"),
    @NamedQuery(name = "Person.findByLastName", query = "SELECT s FROM Person s WHERE s.lastName = :lastName")
})
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PersonId")
    private Long id;
    
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    
    @Basic(optional = false)
    @Column(name = "firstName")
    private String firstName;
    
    @Basic(optional = false)
    @Column(name = "lastName")
    private String lastName;
    
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Professor professor;
    
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Student student;
    
    private boolean isStudent = false;
    private boolean isProfessor = false;
    
    public Person() {    
    }
    
    public Person(String password, String firstName, String lastName) {  
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Professor getProfessor() {
        return professor;
    }

    public void setProfessor(Professor professor) {
        if(!isStudent){
            isProfessor = true;
            this.professor = professor;
        }
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        if(!isProfessor){
            isStudent = true;
            this.student = student;
        }
    }

    public String getPassword() {
        return password;
    }

    public boolean isIsProfessor() {
        return isProfessor;
    }

    public void setIsProfessor(boolean isProfessor) {
        this.isProfessor = isProfessor;
    }

    public boolean isIsStudent() {
        return isStudent;
    }

    public void setIsStudent(boolean isStudent) {
        this.isStudent = isStudent;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getFirstName() + " " + getLastName() + " (" + id + ")";
    }
    
}
